package znbkTopicType.resContent;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.example.lancooznbk.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.List;

import znbkTopicType.WrapBaseAdapter;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperInfor.ZnbkPaperInfor;
import znbkTestPaperRdtz.AudioTextJson;

import static znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity.mEntityRdtz;
import static znbkTestPaperSc.activity.CollectionPaperActivity.mEntitySc;

/**
 * Created by Administrator on 2018\3\16 0016.
 */

public class ZnbkContentResSplAdapter extends WrapBaseAdapter {

    private LayoutInflater mInflater;
    private Context mContext;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试卷的ID
    private int PaperID;
    private int ModelFrom;//试卷来源

    private ViewHolder holder = null;
    private class ViewHolder{

        WebView WvTheme;//大题文本题干区

        private ViewHolder(View convertView) {

            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);

        }
    }

    /*
   * 获取小题选项信息
   * */
    public ZnbkContentResSplAdapter(Context context, int sumItem, int paperID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.ModelFrom = modelFrom;
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_content_spl_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /*
        * 获取作文题用户相关信息
        * */
        getQuesInfor();

        /*
        * 显示题干信息
        * */
        showQuesInfor();


        return convertView;
    }


    /*
    * 获取作文题用户相关信息
    * */
    private String ResName = "";
    private String ResTypeName = "";
    private String VideoPath = "";
    private String TextPath = "";
    private String htmlStr = "";
    private void  getQuesInfor(){

        if(ModelFrom== ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION) {
                /*
                * 显示资源概况
                * */
            ResName = mEntityRdtz.getResFromTK().getResName();
            if (ResName.equals("")) {
                ResName = mEntityRdtz.getResFromTK().getResTypeName();
            }


                /*
                * 视频路径
                * */
            VideoPath = mEntityRdtz.getResFromTK().getVedioList().get(0).getAudioPath();

                /*
                * 音频对应的文本路径
                * */
            TextPath = mEntityRdtz.getResFromTK().getAudioList().get(0).getTextPath();
//            getYplText(TextPath);

        }else if(ModelFrom==ZnbkPaperDef.FROM_COLLECTION){
                /*
                * 显示资源概况
                * */
            ResName = mEntitySc.getResDetail().getResName();
            if (ResName.equals("")) {
                ResName = mEntitySc.getResDetail().getResTypeName();
            }

                /*
                * 资源类型
                * */
            ResTypeName = mEntitySc.getResDetail().getResTypeName();

                /*
                * 视频路径
                * */
            VideoPath = mEntitySc.getResDetail().getVedioList().get(0).getAudioPath();
//            player.play(VideoPath);
//            player.setShowNavIcon(false);

                /*
                * 音频对应的文本路径
                * */
            TextPath = mEntitySc.getResDetail().getVedioList().get(0).getTextPath();
//            getYplText(TextPath);

        }

    }

    /*
    * 显示界面信息
    * */
    private void showQuesInfor(){


    }


    //利用Handler更新UI
    final Handler h = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0x123&!htmlStr.equals("")){
//                holder.WvTheme.setText(msg.obj.toString());
//                Log.e("====wgl=====",""+msg.obj.toString());
                Gson gson = new Gson();
                List<AudioTextJson> listAudioTextJson = gson.fromJson(htmlStr, new TypeToken<List<AudioTextJson>>(){}.getType());
                String audioTextAll = "";
                for(int i=0;i<listAudioTextJson.size();i++){

                    audioTextAll = audioTextAll + listAudioTextJson.get(i).getContent()+"</P>";

                }
                holder.WvTheme.loadData(audioTextAll,"text/html","UTF-8");
            }
        }
    };


    class AccessNetwork implements Runnable{
        private Handler h;

        public AccessNetwork(Handler h) {
            super();
            this.h = h;
        }

        @Override
        public void run() {
            Message m = new Message();
            m.what = 0x123;
            h.sendMessage(m);
        }
    }

    private void getYplText(String Path){

        String url = ZnbkPaperInfor.BaseHttpAdress + "api/ZNBK/W_ZNBK_GetTextInfo?" + "path=" + Path + "&IsAbsoluteUrl=true";

        /**
         * OkHttpClient 的Get请求
         */

        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
            }

            @Override
            public void onResponse(final Response response) throws IOException {
                htmlStr = response.body().string();
                Log.e("==wgl==", "" + htmlStr);
            }
        });
    }

}